﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace Client
{
    class Program
    {
        public const char delim = ' ';
        public static AutoResetEvent ev;
        public static bool authorized;

        static void Main(string[] args)
        {
            Console.WriteLine("Hello Client!");
            authorized = false;
            TCP obj=new TCP();
            while (true)
            {
                ev = new AutoResetEvent(false);
                Console.WriteLine("Enter command:");
                string command=Console.ReadLine();
                if (command == "connect")
                {
                    if (!obj.Connected())
                    {
                        obj.Start();
                        while (!obj.Connected()) { }
                        if (obj.Connected())
                        {
                            Console.WriteLine("Enter your login:");
                            string login = Console.ReadLine();
                            Console.WriteLine("Enter your password:");
                            string password = Console.ReadLine();
                            obj.Send("login " + login + " " + password);
                            ev.WaitOne();
                        }
                        else
                            Console.WriteLine("Can't connect to server!!! Server does not respond!!!");
                    }
                    if (!authorized)
                        command = "disconnect";
                }
                if (command == "clear" || command == "cls")
                {
                    Console.Clear();
                    continue;
                }
                if (command == "disconnect")
                {
                    if (obj.Connected())
                    {
                        Console.WriteLine("Disconnection from the server...");
                        authorized = false;
                        obj.Send("disconnect");
                        Thread.Sleep(500);
                        obj.Disconnect();
                        Console.WriteLine("Disconnected");
                    }
                    continue;
                }
                if (command == "exit")
                {
                    if (obj.Connected())
                    {
                        obj.Send("disconnect");
                        Thread.Sleep(500);
                        obj.Disconnect();
                    }
                    break;
                }
                if (authorized)
                {
                    if (command == "send")
                    {
                        if (obj.Connected())
                        {
                            Console.WriteLine("Enter message for server");
                            obj.Send(Console.ReadLine());
                        }
                        else
                            Console.WriteLine("You can't send message, because you are not connected to the server");
                        continue;
                    }
                    if (command == "cstatus")
                    {
                        if (obj.Connected())
                            Console.WriteLine("Connected to the server");
                        else
                            Console.WriteLine("Offline regime!!!");
                        continue;
                    }
                    if (command == "online_users")
                    {
                        if (obj.Connected())
                            obj.Send(command);
                        continue;
                    }
                    if (command == "offline_users")
                    {
                        if (obj.Connected())
                            obj.Send(command);
                        continue;
                    }
                    if (command == "all_users")
                    {
                        if (obj.Connected())
                            obj.Send(command);
                        continue;
                    }
                    if (command == "del_user")
                    {
                        if (obj.Connected())
                        {
                            Console.WriteLine("Enter login of the user you want to remove from data base:");
                            obj.Send(command + " " + Console.ReadLine());
                        }
                        continue;
                    }
                    if (command == "add_user")
                    {
                        if (obj.Connected())
                        {
                            Console.WriteLine("Enter name of new user:");
                            string name = Console.ReadLine();
                            Console.WriteLine("Enter surname of new user:");
                            string surname = Console.ReadLine();
                            Console.WriteLine("Enter login of new user:");
                            string login = Console.ReadLine();
                            Console.WriteLine("Enter password of new user:");
                            string password = Console.ReadLine();
                            Console.WriteLine("Confirm password:");
                            string confirm = Console.ReadLine();
                            Console.WriteLine("Enter ID of new user:");
                            int ID = Convert.ToInt32(Console.ReadLine());
                            obj.Send(command + delim + name + delim + surname + delim + login + delim + password + delim + confirm + delim + ID);
                        }
                        continue;
                    }
                    if (command == "change_us_rights")
                    {
                        if (obj.Connected())
                        {
                            Console.WriteLine("Enter login of the user you want to change rights:");
                            obj.Send(command + " " + Console.ReadLine());
                        }
                        continue;
                    }
                }
                else
                {
                    Console.WriteLine("You can't use any command because you are not authorized!!!");
                    Console.WriteLine("To connect to the server type 'connect'");
                }

            }
            Environment.Exit(1);
        }
    }
}
